﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.BLL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Configuration;
using System.Data;
using IHISCETeim.DAL;

namespace IHISCETeim.SendEMailSvr
{
    public class EMailController
    {
        /// <summary>
        /// 处理模具分摊中 基准BOM 已不用的物料
        /// </summary>
        public static void SendPToolingChargeNoBom()
        {           

            //1.表 PToolingCharge
            DataTable dt1 = SyncSAPDAL.GetPToolingChargeNoUseBom();
            DataTable dt2 = SyncSAPDAL.GetPToolingChargeDetailNoUseBom();
            if (dt1.Rows.Count <= 0 && dt2.Rows.Count<=0)
            {
                return;
            }


            string bodyHtml = "大家好:<br/>&nbsp;&nbsp;&nbsp;";
            bodyHtml += "以下为信息化系统自动扫描的 模具费分摊 中相应物料已在基准BOM不使用的物料列表,请及时核对是否已设变,如果是请进入系统添加相应的分摊物料<b>(本提醒只发送一次)</b>.";
            bodyHtml += "<table border=1 style=\" border-color:#000;\"><tr>";
            bodyHtml += string.Format("<td>{0}</td>", "物料号");
            bodyHtml += string.Format("<td>{0}</td>", "物料名称");
            bodyHtml += "</tr>";

            foreach (DataRow dr in dt1.Rows)
            {
                bodyHtml += "<tr>";

                bodyHtml += string.Format("<td>{0}</td>", dr["ItemCode"]);
                bodyHtml += string.Format("<td>{0}</td>", dr["ItemName"]);               

                bodyHtml += "</tr>";
               SyncSAPDAL.UpdatePToolingChargeIsSendNoBom(Convert.ToInt32(dr["id"]));//更新已发送,下次不在提示
            }

            foreach (DataRow dr in dt2.Rows)
            {
                bodyHtml += "<tr>";

                bodyHtml += string.Format("<td>{0}</td>", dr["ItemCode"]);
                bodyHtml += string.Format("<td>{0}</td>", dr["ItemName"]);

                bodyHtml += "</tr>";
                SyncSAPDAL.UpdatePToolingChargeDetailIsSendNoBom(Convert.ToInt32(dr["id"]));//更新已发送,下次不在提示
            }
            bodyHtml += "</table>";

            string PToolingChargeEmailToMail = ConfigurationManager.AppSettings["PToolingChargeNoBomToMail"];
            string PToolingChargeEmailSubject ="模具分摊中在基准BOM已不用的物料清单_请核对设变";
            PToolingChargeEmailSubject += DateTime.Now.ToString("yyyy-MM-dd");
            SEMailListController.Add(PToolingChargeEmailToMail, PToolingChargeEmailSubject, bodyHtml);
        }

        /// <summary>
        /// 获取模具分摊费提示邮件正文
        /// </summary>
        /// <returns></returns>
        public static void GetPToolingChargeExpireList()
        {
           

           

            List<PToolingChargeInfo> listEx = PToolingChargeController.GetExpireList();
            if (listEx.Count <= 0)
            {
                return ;
            }

            string bodyHtml = "大家好:<br/>&nbsp;&nbsp;&nbsp;";
            bodyHtml += "以下为信息化系统自动扫描的 模具费分摊 采购量已到达要求的物料列表,请及时提交<b>物料主数据变更申请单</b>.并及时在信息化系统上更新已分摊";
            bodyHtml += "<table border=1 style=\" border-color:#000;\"><tr>";
            bodyHtml += string.Format("<td>{0}</td>","物料号");
            bodyHtml += string.Format("<td>{0}</td>", "物料名称");
            bodyHtml += string.Format("<td>{0}</td>", "供应商");
            bodyHtml += string.Format("<td>{0}</td>", "已采购数量");
            bodyHtml += string.Format("<td>{0}</td>", "分摊数量");
            bodyHtml += string.Format("<td>{0}</td>", "设变后分摊物料");
            bodyHtml += "</tr>";


            foreach (PToolingChargeInfo itm in listEx)
            {
                bodyHtml += "<tr>";

                bodyHtml += string.Format("<td>{0}</td>", itm.ItemCode);
                bodyHtml += string.Format("<td>{0}</td>", itm.ItemName);
                bodyHtml += string.Format("<td>{0}</td>", itm.SupplerName);
                bodyHtml += string.Format("<td>{0}</td>", itm.CurrBuyNum);
                bodyHtml += string.Format("<td>{0}</td>", itm.NeedBuyNum);
                bodyHtml += string.Format("<td>{0}</td>", itm.detailItemCode);
                bodyHtml += "</tr>";
            }

            bodyHtml += "</table>";

            //return bodyHtml;

            string PToolingChargeEmailToMail = ConfigurationManager.AppSettings["PToolingChargeEmailToMail"];
            string PToolingChargeEmailSubject = ConfigurationManager.AppSettings["PToolingChargeEmailSubject"];
            PToolingChargeEmailSubject += DateTime.Now.ToString("yyyy-MM-dd");
            SEMailListController.Add(PToolingChargeEmailToMail, PToolingChargeEmailSubject, bodyHtml);
        }
        /// <summary>
        /// 扫描需发送给生管部的生产项目
        /// </summary>
        public static void GetNeedSendSCGLOPrject()
        {
            DateTime dtnow = DateTime.Now;
            if (dtnow.Hour != 5 || dtnow.Minute != 35)
            {
                return;
            }

            if ((dtnow.Second == 10 || dtnow.Second == 11 || dtnow.Second == 12 || dtnow.Second == 13 || dtnow.Second == 14 || dtnow.Second == 15))
            {
                return;
            }


            List<OProjectInfo> listEx = OProjectController.GetNeedSendEmail();
            if (listEx==null || listEx.Count<=0)
            {
                return;
            }

            string bodyHtml = "大家好:<br/>&nbsp;&nbsp;&nbsp;";
            bodyHtml += "生产部将在最近生产以下机器,具体如下表,请即时确认及更新SAP系统相关生产订单，以便生产部进行生产.<BR/>";
            bodyHtml += "<table border=1 style=\" border-color:#000;\"><tr>";
            bodyHtml += string.Format("<td>{0}</td>", "机型");
            bodyHtml += string.Format("<td>{0}</td>", "项目名称");
            bodyHtml += string.Format("<td>{0}</td>", "起始机号");
            bodyHtml += string.Format("<td>{0}</td>", "结束机号");
            bodyHtml += string.Format("<td>{0}</td>", "台数");
            bodyHtml += "</tr>";


            foreach (OProjectInfo itm in listEx)
            {
                bodyHtml += "<tr>";

                bodyHtml += string.Format("<td>{0}</td>", itm.MachineType);
                bodyHtml += string.Format("<td>{0}</td>", itm.PName);
                bodyHtml += string.Format("<td>{0}</td>",itm.TypeSuff+itm.StartNum);
                bodyHtml += string.Format("<td>{0}</td>", itm.TypeSuff + itm.EndNum);
                bodyHtml += string.Format("<td>{0}</td>", itm.MacNum);

                bodyHtml += "</tr>";
                OProjectController.UpdateSendEmail(itm.PID);
            }

            bodyHtml += "</table>";

            string OProjectEmailToMail = ConfigurationManager.AppSettings["OProjectEmailToMail"];
            string OProjectEmailSubject = ConfigurationManager.AppSettings["OProjectEmailSubject"];
            OProjectEmailSubject += DateTime.Now.ToString("yyyy-MM-dd");
            SEMailListController.Add(OProjectEmailToMail, OProjectEmailSubject, bodyHtml);
        }

        public static void GetMonthNeedRemind()
        {
            

            List<SPapersRemindInfo> listEx = SPapersRemindController.GetMonthNeedRemind();
            if (listEx == null || listEx.Count <= 0)
            {
                return;
            }

            Dictionary<int, List<SPapersRemindInfo>> groupbyList = new Dictionary<int, List<SPapersRemindInfo>>();
            List<int> allUserId = new List<int>();
            foreach (SPapersRemindInfo gItem in listEx)
            {
                if (!allUserId.Contains(gItem.CreateUserId))
                    allUserId.Add(gItem.CreateUserId);
            }

            List<SPapersRemindInfo> groupUserList = new List<SPapersRemindInfo>();
            foreach (int userIdItem in allUserId)
            {
                groupUserList = new List<SPapersRemindInfo>();
                groupUserList = listEx.FindAll(e => e.CreateUserId == userIdItem);
                groupbyList.Add(userIdItem, groupUserList);
            }

            foreach (var groupItem in groupbyList)
            {
                //邮件创建
                int CreateUserId = groupItem.Key;
                List<SPapersRemindInfo> RemindList = groupItem.Value;
                OUserInfo userObj = OUserController.GetById(CreateUserId);
                string bodyHtml = "TO:" + userObj.RealName+ "<br/>&nbsp;&nbsp;&nbsp;";
                bodyHtml += "以下证件需在本月内年审,请注意.<BR/>";
                bodyHtml += "<table border=1 style=\" border-color:#000;\"><tr>";
                bodyHtml += string.Format("<td>{0}</td>", "部门");
                bodyHtml += string.Format("<td>{0}</td>", "员工");
                bodyHtml += string.Format("<td>{0}</td>", "证件号");
                bodyHtml += string.Format("<td>{0}</td>", "证件名称");
                bodyHtml += string.Format("<td>{0}</td>", "办证日期");
                bodyHtml += string.Format("<td>{0}</td>", "年审日期");
                bodyHtml += string.Format("<td>{0}</td>", "有效日期");
                bodyHtml += string.Format("<td>{0}</td>", "发证部门");

                bodyHtml += "</tr>";


                foreach (SPapersRemindInfo itm in RemindList)
                {
                    bodyHtml += "<tr>";

                    bodyHtml += string.Format("<td>{0}</td>", itm.Department);
                    bodyHtml += string.Format("<td>{0}</td>", itm.EmployeeName);
                    bodyHtml += string.Format("<td>{0}</td>", itm.IDNO);
                    bodyHtml += string.Format("<td>{0}</td>", itm.IDName);
                    bodyHtml += string.Format("<td>{0}</td>", itm.RushDate);
                    bodyHtml += string.Format("<td>{0}</td>", itm.YearCarefulDate);
                    bodyHtml += string.Format("<td>{0}</td>", itm.ExpiryDate);
                    bodyHtml += string.Format("<td>{0}</td>", itm.Licensedepartment);

                    bodyHtml += "</tr>";
                }

                bodyHtml += "</table>";

                string OProjectEmailToMail = userObj.EMail;
                string OProjectEmailSubject = string.Format("提醒:本月[{0}]需年审的证件列表",DateTime.Now.ToString("yyyy-MM"));
             
                SEMailListController.Add(OProjectEmailToMail, OProjectEmailSubject, bodyHtml);
            }
        }

        public static void GetDayNeedRemind()
        {
            

            List<SPapersRemindInfo> listEx = SPapersRemindController.GetDayNeedRemind();
            if (listEx == null || listEx.Count <= 0)
            {
                return;
            }

            Dictionary<int, List<SPapersRemindInfo>> groupbyList = new Dictionary<int, List<SPapersRemindInfo>>();
            List<int> allUserId = new List<int>();
            foreach (SPapersRemindInfo gItem in listEx)
            {
                if (!allUserId.Contains(gItem.CreateUserId))
                    allUserId.Add(gItem.CreateUserId);
            }

            List<SPapersRemindInfo> groupUserList = new List<SPapersRemindInfo>();
            foreach (int userIdItem in allUserId)
            {
                groupUserList = new List<SPapersRemindInfo>();
                groupUserList = listEx.FindAll(e => e.CreateUserId == userIdItem);
                groupbyList.Add(userIdItem, groupUserList);
            }

            foreach (var groupItem in groupbyList)
            {
                //邮件创建
                int CreateUserId = groupItem.Key;
                List<SPapersRemindInfo> RemindList = groupItem.Value;
                OUserInfo userObj = OUserController.GetById(CreateUserId);
                string bodyHtml = "TO:" + userObj.RealName + "<br/>&nbsp;&nbsp;&nbsp;";
                bodyHtml += "以下证件将在近期需年审,请注意.<BR/>";
                bodyHtml += "<table border=1 style=\" border-color:#000;\"><tr>";
                bodyHtml += string.Format("<td>{0}</td>", "部门");
                bodyHtml += string.Format("<td>{0}</td>", "员工");
                bodyHtml += string.Format("<td>{0}</td>", "证件号");
                bodyHtml += string.Format("<td>{0}</td>", "证件名称");
                bodyHtml += string.Format("<td>{0}</td>", "办证日期");
                bodyHtml += string.Format("<td>{0}</td>", "年审日期");
                bodyHtml += string.Format("<td>{0}</td>", "有效日期");
                bodyHtml += string.Format("<td>{0}</td>", "发证部门");

                bodyHtml += "</tr>";


                foreach (SPapersRemindInfo itm in RemindList)
                {
                    bodyHtml += "<tr>";

                    bodyHtml += string.Format("<td>{0}</td>", itm.Department);
                    bodyHtml += string.Format("<td>{0}</td>", itm.EmployeeName);
                    bodyHtml += string.Format("<td>{0}</td>", itm.IDNO);
                    bodyHtml += string.Format("<td>{0}</td>", itm.IDName);
                    bodyHtml += string.Format("<td>{0}</td>", itm.RushDate);
                    bodyHtml += string.Format("<td>{0}</td>", itm.YearCarefulDate);
                    bodyHtml += string.Format("<td>{0}</td>", itm.ExpiryDate);
                    bodyHtml += string.Format("<td>{0}</td>", itm.Licensedepartment);

                    bodyHtml += "</tr>";
                }

                bodyHtml += "</table>";

                string OProjectEmailToMail = userObj.EMail;
                string OProjectEmailSubject = string.Format("提醒:[{0}]近期需年审的证件列表", DateTime.Now.ToString("yyyy-MM-dd"));

                SEMailListController.Add(OProjectEmailToMail, OProjectEmailSubject, bodyHtml);
            }
        }

        /// <summary>
        /// 处理DWF图纸审批提示相关
        /// </summary>
        public static void JSDWFApplyGetNeedSendEmail()
        {
            List<JSDWFApplyInfo> applyList = JSDWFApplyController.GetNeedSendEmail();
            if (applyList == null)
                return;
            

            foreach (JSDWFApplyInfo itm in applyList)
            {
                OUserInfo ApplyUser = OUserController.GetByUserName(itm.ApplyUserName);//申请人员
                JSDWFFlowConfigInfo flowObj=JSDWFFlowConfigController.GetByFlowId(itm.FlowID,itm.AdtlvlID);
                OUserInfo currFlowUser = OUserController.GetByUserName(flowObj.UserName);//审批人员

                string applydwfurl = ConfigurationManager.AppSettings["applydwfurl"];//DWF图纸审批URL

                string mailto = "";
                string mailsubject = "";//主题
                string mailbody = "";//内容
                switch (itm.ApplyState)
                { 
                    case 0://审批中
                        mailsubject = string.Format("{0}申请DWF图纸",ApplyUser.RealName);
                        mailto = currFlowUser.EMail;
                        mailbody = string.Format("{0},{1}正在申请使用以下DWF图纸.用途为：{2}",currFlowUser.RealName,ApplyUser.RealName,itm.Remark);
                        mailbody += "<br/>以下为本次申请的图纸列表.<br/>";
                        mailbody += GetApplyDetail(itm.ApplyGUID);
                        mailbody +=string.Format("<br/>快捷批复=>&nbsp;&nbsp;&nbsp;&nbsp;<a target='_blank' href='{0}?g={1}&s=1' style='font-size:14px;font-weight:bold;'>同意</a>&nbsp;&nbsp;&nbsp;&nbsp;",applydwfurl,itm.ApplyGUID);//通过审批
                        mailbody +=string.Format("&nbsp;&nbsp;&nbsp;&nbsp;<a target='_blank' href='{0}?g={1}&s=2' style='font-size:14px;font-weight:bold; color:red;'>拒绝</a>&nbsp;&nbsp;&nbsp;&nbsp;<br/>",applydwfurl,itm.ApplyGUID);//拒绝审批
                        break;
                    case 1://完成审批 。审批通过
                        mailsubject = string.Format("{0},你于{1}申请的DWF图纸已通过审批", ApplyUser.RealName,itm.ApplyDate.ToString("yyyy-MM-dd"));
                        mailto = ApplyUser.EMail;
                        mailbody = string.Format("{0},你于{1}申请的DWF图纸已通过审批<br/>请 登陆信息化管理平台 查看你的图纸.", ApplyUser.RealName, itm.ApplyDate.ToString("yyyy-MM-dd"));
                        break;
                    case 2://审批拒绝,不进入一下审批

                        mailsubject = string.Format("{0},你于{1}申请的DWF图纸已被拒绝", ApplyUser.RealName,itm.ApplyDate.ToString("yyyy-MM-dd"));
                        mailto = ApplyUser.EMail;
                        mailbody = string.Format("{0},你于{1}申请的DWF图纸已被{2}拒绝.", ApplyUser.RealName, itm.ApplyDate.ToString("yyyy-MM-dd"),currFlowUser.RealName);
                        break;
                }
                SEMailListController.Add(mailto, mailsubject, mailbody);//添加邮件列表
                JSDWFApplyController.UpdateSendEmailState(itm.ApplyGUID);//更新邮件已发送
            }
        }

        private static string GetApplyDetail(Guid ApplyGUID)
        {
            DataTable detaildt = JSDWFApplyController.GetDetailByApplyGUID(ApplyGUID);

            string applydwfviewurl = ConfigurationManager.AppSettings["applydwfviewurl"];//DWF图纸审批 查看DWF图纸URL

            string bodyHtml = "";
            bodyHtml += "<table border=1 style=\" border-color:#000;\"><tr>";
            bodyHtml += string.Format("<td>{0}</td>", "所在分类目录");
            bodyHtml += string.Format("<td>{0}</td>", "图纸名称");
            bodyHtml += string.Format("<td>{0}</td>", "图纸相关说明");
            bodyHtml += string.Format("<td>{0}</td>", "查看");
            bodyHtml += "</tr>";


            foreach (DataRow itm in detaildt.Rows)
            {
                bodyHtml += "<tr>";

                bodyHtml += string.Format("<td>{0}</td>", itm["FolderName"]);
                bodyHtml += string.Format("<td>{0}</td>", itm["FileName"]);
                bodyHtml += string.Format("<td>{0}</td>", itm["Remark"]);
                bodyHtml += string.Format("<td><A target='_blank' href='{1}?g={2}'>{0}</A></td>", "查看", applydwfviewurl, ApplyGUID);

                bodyHtml += "</tr>";
            }

            bodyHtml += "</table>";
            return bodyHtml;
        }


        /// <summary>
        /// 开始扫描BOM变更中关注的未添加物料
        /// 每小时扫描一次
        /// 每次将已添加的做为一封邮件发送
        /// </summary>
        public static void SBOMChangeOITM()
        {
           

            DataTable dt = SBOMChangeOITMController.GetAll();
            string mailto = ConfigurationManager.AppSettings["SBOMChangeOITMtoMail"];//收件人
            string mailsubject =string.Format( "已添加的<BOM变更申请表>所需物料列表{0}(勿回复)",DateTime.Now.ToString("yyyy-MM-dd hh:mm"));//主题
            StringBuilder mailbody=new StringBuilder() ;//内容
            mailbody.AppendLine("以下为你关注的 BOM变更申请表 所需的物料已添加的部份:<BR/>");
            mailbody.AppendLine("<table border='1' style=\" border-color:#000;\"><tr><th>物料号</th><th>物料名称</th></tr>");
            int id = 0;
            string itemcode = "";
            string itemname = "";

            bool needsend = false;

            foreach (DataRow dr in dt.Rows)
            {
                

                id =Convert.ToInt32( dr["id"]);
                itemcode = dr["itemcode"].ToString();
                itemname = SBOController.GetHaveItemCode(itemcode);
                if (itemname!="")
                {
                    needsend = true;

                    mailbody.AppendLine(string.Format("<tr><td>{0}</TD><td>{1}</td></tr>",itemcode,itemname));

                    SBOMChangeOITMController.Send(id);//标记已发送
                }

            }

            mailbody.AppendLine("</table>.<br/>");
            if(needsend)
                SEMailListController.Add(mailto, mailsubject, mailbody.ToString());

        }

        /// <summary>
        /// 货时间、到货时间及故障排除时间如果3天内未填入，是否可以每天发一份邮件跟催询问
        /// 每天早上8点30发邮件
        /// </summary>
        public static void AgentQltySendWarnInputDateEmail()
        {
            

            List<AgentQltyInfo> list = null;
            string mailto = "";
            string mailsubject = "";
            string mailbody = "";
            //1.发货时间 =>索赔发货  
            //list = AgentQltyController.GetWarnInputDate(1);
            //List<AgentQltyInfo>  listSP = list.Where(e => e.PartsRequestType == "索赔发货").ToList();
            //mailto = AgentConfigController.PartsManagerEmails;
            //mailsubject = string.Format("品情系统提醒:有{0}个品情需要您及时维护配件发货信息", list.Count);
            //mailbody = "相应品情如下,请进入 信息平台 进行维护<br/>";
            //mailbody += "<table border='1'>";
            //mailbody += "<tr><td>品情号</td><td>机号</td><td>通知配件发送时间</td></tr>";
            //foreach (AgentQltyInfo itm in listSP)
            //{
            //    mailbody += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>",itm.ClaimsNumber,itm.SerialNumber,itm.NoticeSendPartsDate.ToString("yyyy-MM-dd"));
            //}
            //mailbody += "</table>";
            //if (listSP.Count > 0)
            //{
            //    SEMailListController.Add(mailto, mailsubject, mailbody.ToString());
            //}
            mailto="";
            mailsubject="";
            mailbody="";
            //1.发货时间 =>供应商发货
          //  list = AgentQltyController.GetWarnInputDate(1);
            List<AgentQltyInfo> listSupper = AgentQltyController.GetWarnInputDate(1);
            mailto = AgentConfigController.IHISCESvrSupperEmails;
            mailsubject = string.Format("品情系统提醒:有{0}个品情需要您及时维护供应商发货信息", listSupper.Count);
            mailbody = "相应品情如下,请进入 信息平台 进行维护<br/>";
            mailbody += "<table border='1'>";
            mailbody += "<tr><td>品情号</td><td>机号</td><td>通知发送时间</td></tr>";
            foreach (AgentQltyInfo itm in listSupper)
            {
                mailbody += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", itm.ClaimsNumber, itm.SerialNumber, itm.NoticeSendPartsDate.ToString("yyyy-MM-dd"));
            }
            mailbody += "</table>";
            mailbody += "<BR/>以上内容为 IHISCE 信息平台系统自动发送，请勿回复.";
            if (listSupper.Count > 0)
            {
                SEMailListController.Add(mailto, mailsubject, mailbody.ToString());
            }

            mailto="";
            mailsubject="";
            mailbody="";
            //2.到货时间,经销商
            list = AgentQltyController.GetWarnInputDate(2);
            //listSP = list.Where(e => e.PartsRequestType == "索赔发货").ToList();
            foreach (AgentQltyInfo itm in list)
            {
                mailto = OUserController.GetById(itm.UserId).EMail;
                mailsubject = string.Format("IHISCE品情系统提醒:品情{0}需要您及时维护到货时间", itm.ClaimsNumber);
                mailbody = string.Format("<br/>品情号:{0}<br/>", itm.ClaimsNumber);
                mailbody += string.Format("<br/>机号:{0}<br/>", itm.SerialNumber);
                mailbody += "请进入 IHISCE经销商终端平台 进行维护<br/>";
                mailbody += "<BR/>以上内容为 IHISCE 信息平台系统自动发送，请勿回复.";
                SEMailListController.Add(mailto, mailsubject, mailbody.ToString());

                mailto = "";
                mailsubject = "";
                mailbody = "";
              
            }

            


            //3.故障排除
            list = AgentQltyController.GetWarnInputDate(3);

            foreach (AgentQltyInfo itm in list)
            {
                mailto = OUserController.GetById(itm.UserId).EMail;
                mailsubject = string.Format("IHISCE品情系统提醒:品情{0}需要您及时维护故障排除时间", itm.ClaimsNumber);
                mailbody = string.Format("<br/>品情号:{0}<br/>", itm.ClaimsNumber);
                mailbody += string.Format("<br/>机号:{0}<br/>", itm.SerialNumber);
                mailbody += "请进入 IHISCE经销商终端平台 进行维护<br/>";
                mailbody += "<BR/>以上内容为 IHISCE 信息平台系统自动发送，请勿回复.";
                SEMailListController.Add(mailto, mailsubject, mailbody.ToString());
                mailto = "";
                mailsubject = "";
                mailbody = "";

            }           
          
        }


        public static void SAP_GetDZB()
        {
           


            DataTableCollection dts =  SBOController.ihi_yyx_dzb();
            //呆滞品
            DataTable dt = dts[0];
            string mailto = ConfigurationManager.AppSettings["SBODZB.ToMail"];
            string mailsubject =DateTime.Now.ToString("yyyy-MM-dd")+"[原材料库]呆滞品信息";
            string mailbody = "大家好：<br/>以下为本次系统自动扫描的原材料库呆滞品信息,请既时处理,系统每周会自动扫描发送一次提示邮件.<br/><strong>呆滞品定义:</strong>原材料库有库存并且BOM表不用,生产订单也不用的物料.<br/>";
            mailbody += "<table border='1'>";
            mailbody += "<tr><td>物料号</td><td>物料名称</td><td>呆滞库存数量</td><td>供应商</td></tr>";
            foreach (DataRow dr in dt.Rows)
            {
                mailbody += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", dr["ItemCode"], dr["ItemName"], dr["OnHand"], dr["AliasName"]);
            }
            mailbody += "</table>";

            mailbody += "〈br/〉同时，你也可以到ERP的查询管理器<生产管理部报表>里导出相应的信息.<BR/>以上内容为 系统自动发送，请勿回复.";
            SEMailListController.Add(mailto, mailsubject, mailbody.ToString());
        }

        /// <summary>
        /// 2016-03-17 借件归还报警邮件,每天9:30发
        /// </summary>
        public static void SAP_GetJieJianGuiHuanAlert()
        {
           
          

            //Common.WriteLog(System.DateTime.Now.ToString() + " SAP_GetJieJianGuiHuanAlertScan" + AutoSendEMail.SAP_GetJieJianGuiHuanAlertScan.ToString());
            //if (AutoSendEMail.SAP_GetJieJianGuiHuanAlertScan)
            //    return;

            AutoSendEMail.SAP_GetJieJianGuiHuanAlertScan = true;

            Common.WriteLog(System.DateTime.Now.ToString() + "进入扫描 ");
            DataTableCollection DTS=  SBODAL.GetJieJianGuiHuanAlert();
            DataTable deptDT = DTS[0];
            DataTable owtrList = DTS[1];
            Common.WriteLog(System.DateTime.Now.ToString() + owtrList.Rows.Count.ToString()+"条数");
            foreach (DataRow deptrow in deptDT.Rows)
            {
                string deptname = deptrow["FldValue"].ToString();
                string toemail = SAPConfigController.SAPJieJian_GetDeptEmail(deptname.Trim());
                toemail+=";"+ SAPConfigController.SAPJieJian_GetDeptEmail("cc");
                string subject = deptname+"借件需归还提醒,请注意";
                string boby = "";
                DataRow[] drs = owtrList.Select(string.Format("U_JieJianBuMen='{0}'", deptname));
                foreach (DataRow dr in drs)
                {
                    boby += string.Format("于{0}向原材料库借{1}({2}){3}个,原预计{4}归还，即将或已到期，请及时归还.相应ERP库存转储单号为：{5}",Convert.ToDateTime(dr["taxdate"]).ToString("yyyy-MM-dd"),dr["itemcode"], dr["Dscription"], dr["Quantity"], Convert.ToDateTime(dr["U_GuiHuanShiJian"]).ToString("yyyy-MM-dd"), dr["docnum"]);
                    boby += "<br/>";
                }

                boby += "<BR/>以上内容为 IHISCE 信息平台系统自动发送，请勿回复.";
                if (drs.Count() > 0) { 
                    SEMailListController.Add(toemail, subject, boby);
                }
            }

        }
    }
}
